package com.whpost.service.service.t;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whpost.service.dao.t.TMonthDao;
import com.whpost.service.dao.t.TWeekDao;
import com.whpost.service.entity.t.TMonth;
import com.whpost.service.entity.t.TWeek;
import com.whpost.service.tools.ToolBean;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.temporal.WeekFields;
import java.util.*;

@Service
public class InitDay {

    @Resource
    private TWeekDao tWeekDao;

    @Resource
    private TMonthDao tMonthDao;

    @Transactional
    public String init(String year){
        try
        {
            ToolBean.info("获取的年份："+year);
            int weekSl = ToolBean.getWeekSl(Integer.parseInt(year));
            ToolBean.info("获取的年份："+year+"包含"+weekSl+"个周");

            tWeekDao.delete(new QueryWrapper<TWeek>().eq("year",year));
            tMonthDao.delete(new QueryWrapper<TMonth>().eq("year",year));

            TWeek tWeek = new TWeek();
            TMonth tMonth = new TMonth();

            for (int i=1;i<=weekSl;i++){
                tWeek = new TWeek();
                tWeek.setYear(Long.parseLong(year));
                tWeek.setXh(Long.parseLong(String.valueOf(i)));
                // 获取该周的开始时间（周一）
                WeekFields weekFields = WeekFields.of(Locale.getDefault());
                LocalDate startOfWeek = LocalDate.of(Integer.parseInt(year), 1, 1)
                        .with(weekFields.weekOfYear(), i)
                        .with(weekFields.dayOfWeek(), 2);

                // 获取该周的结束时间（周日）
                LocalDate endOfWeek = startOfWeek.plusDays(6);
                tWeek.setKsrq(Date.from(Timestamp.valueOf(startOfWeek.atTime(LocalTime.MIDNIGHT)).toInstant()));
                tWeek.setJzrq(Date.from(Timestamp.valueOf(endOfWeek.atTime(LocalTime.MIDNIGHT)).toInstant()));
                //ToolBean.info("["+year+"]年第["+i+"]周的时间范围："+startOfWeek+"~"+endOfWeek);
                tWeekDao.insert(tWeek);
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            for (int month = 1; month <=12; month++) {

                YearMonth yearMonth = YearMonth.of(Integer.parseInt(year), month);
                LocalDate lastDay = yearMonth.atEndOfMonth();

                tMonth = new TMonth();
                tMonth.setYear(Long.parseLong(year));
                tMonth.setXh(Long.parseLong(String.valueOf(month)));
                String yf = "";
                if (month<=9)
                    yf = "0"+String.valueOf(month);
                else
                    yf = String.valueOf(month);
                tMonth.setYf(String.valueOf(year)+yf);
                tMonth.setKsrq(sdf.parse(String.valueOf(year)+"-"+yf+"-01"));
                tMonth.setJzrq(Date.from(Timestamp.valueOf(lastDay.atTime(LocalTime.MIDNIGHT)).toInstant()));

                tMonthDao.insert(tMonth);
            }

            return "suc";
        }
        catch(Exception e)
        {
            ToolBean.info("InitDay.init发生错误："+e.toString());
            throw new RuntimeException("InitDay.init！"+e.toString());
        }
    }
}
